package com.wechart.service.manage.impl;



import com.wechart.dao.domain.Areainfo;
import com.wechart.dao.domain.AreainfoCriteria;
import com.wechart.dao.domain.Orginfo;
import com.wechart.dao.domain.OrginfoCriteria;
import com.wechart.dao.mapper.AreainfoMapper;
import com.wechart.dao.mapper.OrginfoMapper;
import com.wechart.dao.mapper.ext.OrginfoExtMapper;
import com.wechart.model.ManagerModel;
import com.wechart.model.SelectModel;
import com.wechart.service.manage.OrginfoService;
import com.wechart.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service(value = "orginfoService")
public class OrginfoServiceImpl implements OrginfoService {

    @Autowired
    private OrginfoMapper orginfoMapper;

    @Autowired
    private OrginfoExtMapper orginfoExtMapper;

    @Autowired
    private AreainfoMapper areainfoMapper;

    @Override
    public List<Orginfo> getOrginfo(ManagerModel managerModel){
        OrginfoCriteria orginfoCriteria=new OrginfoCriteria();
        OrginfoCriteria.Criteria criteria=orginfoCriteria.createCriteria();
        if(StringUtils.isNoNullString(managerModel.getStart())){
            criteria.andCreatedateGreaterThan(StringUtils.cutDateStr(managerModel.getStart()));
        }
        if(StringUtils.isNoNullString(managerModel.getEnd())){
            criteria.andCreatedateLessThan(StringUtils.cutDateStr(managerModel.getEnd()));
        }
        if(StringUtils.isNoNullString(managerModel.getCode())){
            criteria.andOrgcodeEqualTo(managerModel.getCode());
        }
        if(managerModel.getAreacode().equals("3300")&& StringUtils.isNoNullString(managerModel.getAreaname())){
            criteria.andAreacodeEqualTo(StringUtils.getIdFromString(managerModel.getAreaname()));
        }
        if (null!=managerModel.getAreacode() && !managerModel.getAreacode().equals("3300")){
            criteria.andAreacodeEqualTo(managerModel.getAreacode());
        }
        if(StringUtils.isNoNullString(managerModel.getOrgname())){
            criteria.andOrgnameEqualTo(managerModel.getOrgname());
        }
        orginfoCriteria.setOrderByClause("id asc");
        List<Orginfo> data = new ArrayList<>();
        for (Orginfo orginfo:orginfoMapper.selectByExample(orginfoCriteria)){
            AreainfoCriteria areainfoCriteria = new AreainfoCriteria();
            areainfoCriteria.createCriteria().andCodeEqualTo(orginfo.getAreacode());
            List<Areainfo> areas = areainfoMapper.selectByExample(areainfoCriteria);
            if (areas!=null&&areas.size()>0) {
                orginfo.setAreacode(areainfoMapper.selectByExample(areainfoCriteria).get(0).getName());
            }
            data.add(orginfo);
        }
        return data;
    }

    /**
     * 入网机构信息插入
     * @param managerModel
     * @return
     */
    @Override
    public int insertOrginfo(ManagerModel managerModel) {
        OrginfoCriteria orginfoCriteria=new OrginfoCriteria();
        Orginfo data = new Orginfo();
//        data.setId(Integer.parseInt(managerModel.getId()));
        data.setOrgcode(managerModel.getOrgcode());
        data.setOrgname(managerModel.getOrgname());
//        data.setAreacode(managerModel.getAreacode());
        data.setAreacode(managerModel.getAreacode());
        data.setOrgtype(Short.parseShort(managerModel.getOrgtype()));
        data.setBankcode(managerModel.getBankcode());
        data.setNodecode(managerModel.getNodecode());
        data.setBusiallow(managerModel.getBusiallow());
        data.setConsigntype(Short.parseShort(managerModel.getConsigntype()));
        data.setConsigntag(managerModel.getConsigntag());
        data.setCheckflag(Short.parseShort(managerModel.getCheckflag()));
        data.setMactype(Short.parseShort(managerModel.getMactype()));
        data.setMacindex(Integer.parseInt(managerModel.getMacindex()));
        data.setCertinfo(managerModel.getCertinfo());
        data.setNodetype(Short.parseShort(managerModel.getNodetype()));
        data.setSvctype(Short.parseShort(managerModel.getSvctype()));
        data.setSendhost(managerModel.getSendhost());
        data.setRecvhost(managerModel.getRecvhost());
        data.setServport(Integer.parseInt(managerModel.getServport()));
        data.setCommtype(Short.parseShort(managerModel.getCommtype()));
        data.setOrgstat(Short.parseShort(managerModel.getOrgstat()));
        data.setLoglevel(Short.parseShort(managerModel.getLoglevel()));
        data.setDownmode(Short.parseShort(managerModel.getDownmode()));
        data.setCreatedate(managerModel.getCreatedate());
        data.setModifydate(managerModel.getModifydate());
        return orginfoMapper.insertSelective(data);
    }

    /**
     * 入网机构信息修改
     * @param managerModel
     * @return
     */
    @Override
    public int modifyOrginfo(ManagerModel managerModel ,int id) {
        OrginfoCriteria orginfoCriteria=new OrginfoCriteria();
        orginfoCriteria.createCriteria().andIdEqualTo(id);
        Orginfo data = new Orginfo();
        data.setId(Integer.parseInt(managerModel.getId()));
        data.setOrgcode(managerModel.getOrgcode());
        data.setOrgname(managerModel.getOrgname());
        data.setAreacode(managerModel.getAreacode());
        data.setOrgtype(Short.parseShort(managerModel.getOrgtype()));
        data.setBankcode(managerModel.getBankcode());
        data.setNodecode(managerModel.getNodecode());
        data.setBusiallow(managerModel.getBusiallow());
        data.setConsigntype(Short.parseShort(managerModel.getConsigntype()));
        data.setConsigntag(managerModel.getConsigntag());
        data.setCheckflag(Short.parseShort(managerModel.getCheckflag()));
        data.setMactype(Short.parseShort(managerModel.getMactype()));
        data.setMacindex(Integer.parseInt(managerModel.getMacindex()));
        data.setCertinfo(managerModel.getCertinfo());
        data.setNodetype(Short.parseShort(managerModel.getNodetype()));
        data.setSvctype(Short.parseShort(managerModel.getSvctype()));
        data.setSendhost(managerModel.getSendhost());
        data.setRecvhost(managerModel.getRecvhost());
        data.setServport(Integer.parseInt(managerModel.getServport()));
        data.setCommtype(Short.parseShort(managerModel.getCommtype()));
        data.setOrgstat(Short.parseShort(managerModel.getOrgstat()));
        data.setLoglevel(Short.parseShort(managerModel.getLoglevel()));
        data.setDownmode(Short.parseShort(managerModel.getDownmode()));
        data.setCreatedate(managerModel.getCreatedate());
        data.setModifydate(managerModel.getModifydate());
        return orginfoMapper.updateByExampleSelective(data,orginfoCriteria);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Orginfo getOrginfoById(int id) {
        OrginfoCriteria orginfoCriteria=new OrginfoCriteria();
        orginfoCriteria.createCriteria().andIdEqualTo(id);
        List<Orginfo> data = orginfoMapper.selectByExample(orginfoCriteria);
        if (null!=data) {
            return data.get(0);
        }else{
            return null;
        }
    }

    /**
     * 用地区代码取得机构号
     * @param areacode
     * @return
     */
    @Override
    public List<String> getOrgcodeByAreacode(String areacode){
        OrginfoCriteria orginfoCriteria=new OrginfoCriteria();
        orginfoCriteria.createCriteria().andAreacodeEqualTo(areacode);
        List<Orginfo> data = orginfoMapper.selectByExample(orginfoCriteria);
        List<String> orgcode=new ArrayList<>();
        if (null!=data) {
            for(int i=0;i<data.size();i++){
                orgcode.add(data.get(i).getOrgcode());
            }
            return orgcode;
        }else{
            return null;
        }
    }

    /**
     * 搜索框模糊查询
     * @param name
     * @return
     */
    @Override
    public List<SelectModel> selectOrginfoName(String name){
        List<String> names = orginfoExtMapper.selectForOrginfo(name);
        List<SelectModel> data = new ArrayList<>();
        for (String dn : names){
            SelectModel model = new SelectModel();
            model.setId(dn);
            model.setText(dn);
            data.add(model);
        }
        return  data;
    }

    /**
     * 从数据库中取出机构号和机构名称供select2选择
     * @param name
     * @return
     */
    @Override
    public List<SelectModel> selectOrgidAndName(String name){
        OrginfoCriteria orginfoCriteria=new OrginfoCriteria();
        OrginfoCriteria.Criteria criteria=orginfoCriteria.createCriteria();
        if(StringUtils.isNoNullString(name)){
            criteria.andOrgnameLike("%"+name+"%");
        }
        List<Orginfo> list=orginfoMapper.selectByExample(orginfoCriteria);
        List<SelectModel> data = new ArrayList<>();
        SelectModel model=null;
        model=new SelectModel();
        model.setId(""+"-"+"全部-");
        model.setText(""+"-"+"全部-");
        data.add(model);
        for(Orginfo orginfo:list){
            model=new SelectModel();
            model.setText(orginfo.getOrgcode()+"-"+orginfo.getOrgname());
            model.setId(orginfo.getOrgcode()+"-"+orginfo.getOrgname());
            data.add(model);
        }
        return data;
    }

    /**
     * 用机构号取得机构名称
     * @param orgid
     * @return
     */
    @Override
    public String getOrgNameByOrgId(String orgid){
        String name = orginfoExtMapper.getOrgNameByOrgId(orgid);
       if(StringUtils.isNoNullString(name)){
           return name;
       }else{
           return orgid;
       }
    }

    /**
     * 用机构号取得机构信息
     * @param orgid
     * @return
     */
    public Orginfo getOrginfoByPK(String orgid){
        if(StringUtils.isNullString(orgid)){
            return null;
        }
        OrginfoCriteria orginfoCriteria=new OrginfoCriteria();
        OrginfoCriteria.Criteria criteria=orginfoCriteria.createCriteria();
        criteria.andOrgcodeEqualTo(orgid);
        List<Orginfo> list=orginfoMapper.selectByExample(orginfoCriteria);
        if(list.size()>=1){
            return list.get(0);
        }else {
            return null;
        }
    }

    /**
     *批量导入的插入功能
     * @param fieldsTmp
     * @return
     */
    public int insertOrginfoinfoByArray(String[] fieldsTmp){
        Orginfo data=new Orginfo();
        data.setOrgcode(fieldsTmp[0]);
        data.setOrgname(fieldsTmp[1]);
        data.setAreacode(fieldsTmp[2]);
        data.setOrgtype(Short.parseShort(fieldsTmp[3]));
        data.setBankcode(fieldsTmp[4]);
        data.setNodecode(fieldsTmp[5]);
        data.setBusiallow(fieldsTmp[6]);
        data.setConsigntype(Short.parseShort(fieldsTmp[7]));
        data.setConsigntag(fieldsTmp[8]);
        data.setCheckflag(Short.parseShort(fieldsTmp[9]));
        data.setMactype(Short.parseShort(fieldsTmp[10]));
        data.setMacindex(Integer.parseInt(fieldsTmp[11]));
        data.setCertinfo(fieldsTmp[12]);
        data.setNodetype(Short.parseShort(fieldsTmp[13]));
        data.setSvctype(Short.parseShort(fieldsTmp[14]));
        data.setSendhost(fieldsTmp[15]);
        data.setRecvhost(fieldsTmp[16]);
        data.setServport(Integer.parseInt(fieldsTmp[17]));
        data.setCommtype(Short.parseShort(fieldsTmp[18]));
        data.setOrgstat(Short.parseShort(fieldsTmp[19]));
        data.setLoglevel(Short.parseShort(fieldsTmp[20]));
        data.setDownmode(Short.parseShort(fieldsTmp[21]));
        data.setCreatedate(fieldsTmp[22]);
        data.setModifydate(fieldsTmp[23]);
        return orginfoMapper.insertSelective(data);
    }




}
